package com.ttg.web.dao.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ttg.common.dto.TagProducer;
import com.ttg.common.entities.BasePage;
import com.ttg.common.dto.TagProducer;
import com.ttg.common.config.DynamicTableNameHelper;
import com.ttg.model.mapper.BankPrivatePrimitiveMapper;
import com.ttg.model.pojo.BankPrivatePrimitive;
import com.ttg.model.pojo.VipRatingConfig;
import com.ttg.web.dao.BankPrivatePrimitiveDao;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author zhangHongYuan
 * @description
 * @date 2024/4/16 16:49
 */
@Service
public class BankPrivatePrimitiveDaoImpl extends ServiceImpl<BankPrivatePrimitiveMapper, BankPrivatePrimitive> implements BankPrivatePrimitiveDao {
    @Override
    public List<BankPrivatePrimitive> getByCstIdAndDate(String cstId, LocalDate localDate) {
        LocalDateTime min = LocalDateTime.of(localDate, LocalTime.MIN);
        LocalDateTime max = LocalDateTime.of(localDate,LocalTime.MAX);
        LambdaQueryWrapper<BankPrivatePrimitive> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BankPrivatePrimitive::getCstId,cstId);
        queryWrapper.between(BankPrivatePrimitive::getUpdateTime,min,max);
        return list(queryWrapper);
    }

    @Override
    public BankPrivatePrimitive getByCstIdAndDateLast(String cstId) {
        LambdaQueryWrapper<BankPrivatePrimitive> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BankPrivatePrimitive::getCstId,cstId);
        queryWrapper.orderByDesc(BankPrivatePrimitive::getUpdateTime);
        queryWrapper.last("LIMIT 1");
        return getOne(queryWrapper);
    }

    @Override
    public void truncate() {
        baseMapper.truncate();
    }

    @Override
    public List<String> getVipCstIdsByVipRatingConfig(VipRatingConfig vipRatingConfig, BasePage basePage) {
        LocalDateTime min = LocalDateTime.of(LocalDate.now().minusDays(1), LocalTime.MIN);
        LocalDateTime max = LocalDateTime.of(LocalDate.now().minusDays(1),LocalTime.MAX);
        return baseMapper.getVipCstIdsByVipRatingConfig(
                basePage,
                vipRatingConfig.getRatingStart(),
                vipRatingConfig.getRatingEnd(),
                max,
                min,
                //todo 筛选出消费金额 笔数类型不参与计算
                1111
                );
    }

    @Override
    public List<Long> getVipIdsByTags(List<TagProducer> tags, String key) {
        return baseMapper.getVipIdsByTags(tags,key);
    }

    @Override
    @Transactional(
            rollbackFor = {Exception.class}
    )
    public boolean saveBatchTypeSplit(Map<String, List<BankPrivatePrimitive>> batMap) {
        for (Map.Entry<String, List<BankPrivatePrimitive>> entry : batMap.entrySet()) {
            Map<String, String> tableSuffix = DynamicTableNameHelper.getTableSuffix();
            tableSuffix.put("tbl_bank_private_primitive", entry.getKey());
            DynamicTableNameHelper.setTableSuffix(tableSuffix);
            saveBatch(entry.getValue());
        }
        return true;
    }


    @Override
    public List<BankPrivatePrimitive> tblBankPrivatePrimitive(String sql,Integer size,Long startId) {
        if (Objects.isNull(size)){
            size=10;
        }

        return baseMapper.tblBankPrivatePrimitive(sql,size,startId);
    }
}
